//
//  WaterFallViewController.m
//  DoubanDemo1
//
//  Created by lanou3g on 15/12/1.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "WaterFallViewController.h"
#import "WaterCollectionViewCell.h"
#import "DataHandle.h"
#import "MovieDetail.h"
#import "UIImage+LoadImage.h"
#import "MovieDetailViewController.h"
@interface WaterFallViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong) UICollectionView *collectionView;
@end

@implementation WaterFallViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"电影";
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(100, 200);
    layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[WaterCollectionViewCell class] forCellWithReuseIdentifier:@"movie"];
    [self.view addSubview:self.collectionView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[DataHandle sharedInstance] rowOfMovie];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WaterCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"movie" forIndexPath:indexPath];
    
    MovieDetail *movieDetail =  [[DataHandle sharedInstance] movieDetailForIndexPath:indexPath];
    cell.button.tag = indexPath.item;
    [cell.button addTarget:self action:@selector(ImageBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    if(nil == movieDetail.imageList && NO == movieDetail.isLoading){
        [movieDetail loadImage];
        [movieDetail addObserver:self forKeyPath:@"imageList" options:NSKeyValueObservingOptionNew context:(__bridge void *)(indexPath)];
    }else {
        [cell.button setBackgroundImage:movieDetail.imageList forState:UIControlStateNormal];
    }
    cell.label.textAlignment = NSTextAlignmentCenter;
    cell.label.text = movieDetail.movieName;
    
    return cell;
}

- (void)ImageBtnAction:(UIButton *)sender
{
    MovieDetailViewController *movieDetailVC = [[MovieDetailViewController alloc] init];
    NSIndexPath *indexPath  = [NSIndexPath indexPathForItem:sender.tag inSection:0];
    movieDetailVC.movieDetail = [[DataHandle sharedInstance] movieDetailForIndexPath:indexPath];
   [[DataHandle sharedInstance] movieID:movieDetailVC.movieDetail.movieId ReloadBlack:^(MovieDetail *model) {
       model.movieId = movieDetailVC.movieDetail.movieId;
       model.movieName = movieDetailVC.movieDetail.movieName;
       model.pic_url = movieDetailVC.movieDetail.pic_url;
       movieDetailVC.movieDetail = model;
       [self.navigationController pushViewController:movieDetailVC animated:YES];
   }];


}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    UIImage *newImage = change[NSKeyValueChangeNewKey];
    if (newImage == nil) {
        return;
    }else{
        NSIndexPath *indexPath = (__bridge NSIndexPath *)(context);
        NSArray *arr = [self.collectionView indexPathsForVisibleItems];
        if ([arr containsObject:indexPath]) {
            WaterCollectionViewCell *cell = (WaterCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
            [cell.button setBackgroundImage:newImage forState:UIControlStateNormal];
            [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
        }
    }
    [object removeObserver:self forKeyPath:@"imageList"];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
